package com.midea.ai.air.common;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

import com.midea.ai.air.R;
import com.midea.ai.air.data.ResultModel;
import com.midea.ai.air.data.login.UserInfo;
import com.midea.ai.air.utils.HttpUtils;
import com.squareup.okhttp.RequestBody;

import org.json.JSONException;

import java.util.LinkedList;
import java.util.List;

/**
 * Activity公用的父类 Created by June Kwok on 2015/12/7.
 */
public class BaseActivity extends AppCompatActivity implements HttpUtils.callBack {
    /**
     * 服务器访问地址
     */
    public static final String baseUrl = "http://172.28.212.163:8080";
    /**
     * 请求回调接口列表
     */
    private List<OnActivityResultListener> mActivityResultListener = new LinkedList<>();

    /**
     * 内部跳转方法
     *
     * @param context
     * @param clazz
     */
    public static void startActivityInternal(Context context, Class<? extends Activity> clazz) {
        Intent intent = new Intent(context, clazz);
        context.startActivity(intent);
    }

    /**
     * 回调接口
     */
    public static interface OnActivityResultListener {
        void onActivityResult(BaseActivity activity, int requestCode, int resultCode, Intent data);
    }

    public static abstract class OnSingleActivityResultListener implements OnActivityResultListener {

        @Override
        public void onActivityResult(final BaseActivity activity, int requestCode, int resultCode, Intent data) {

            handleActivityResult(activity, requestCode, resultCode, data);

            ThreadManager.getInstance().postOnUIHandler(new Runnable() {
                @Override
                public void run() {
                    activity.removeActivityResultListener(OnSingleActivityResultListener.this);
                }
            });
        }

        protected abstract void handleActivityResult(BaseActivity activity, int requestCode, int resultCode, Intent data);
    }

    public void addActivityResultListener(OnActivityResultListener listener) {
        mActivityResultListener.add(listener);
    }

    public void removeActivityResultListener(OnActivityResultListener listener) {
        mActivityResultListener.remove(listener);
    }

    public Context getContext() {
        return this;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        for (OnActivityResultListener listener : mActivityResultListener) {
            listener.onActivityResult(this, requestCode, resultCode, data);
        }
    }

    private HttpUtils mHttp = new HttpUtils(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            //activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_base_actvity);
    }

    @Override
    public boolean get(String url) {
        return mHttp.get(url);
    }

    @Override
    public boolean post(String url, String json) {
        return mHttp.post(url, json);
    }

    @Override
    public boolean post(String url, RequestBody requestBody) {
        return mHttp.post(url, requestBody);
    }

    @Override
    public void onError(String url, Exception e) {
    }

    @Override
    public Object asyncExecute(String url, ResultModel resultModel) {
        return null;
    }

    @Override
    public void onFailure(String url, ResultModel resultModel) {
    }

    @Override
    public void onSucceed(String url, ResultModel resultModel) throws JSONException {
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public UserInfo getUserInfo() {
        return ((AirApplication) getApplication()).getUserInfo();
    }

    /**
     * 登出
     */
    public void logout() {
        ((AirApplication) getApplication()).logout();
    }

    /**
     * 是否登录
     *
     * @return
     */
    public boolean isLogin() {
        return ((AirApplication) getApplication()).isLogin();
    }

    /**
     * 无参数页面跳转
     *
     * @param cls
     */
    public void navigate(Class cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }
}
